<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代码" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收费类型" prop="projectType">
        <el-input
          v-model="queryParams.projectType"
          placeholder="请输入收费类型	"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
        :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['payfee:project:export']"
          >导出</el-button>
      </el-form-item>
    </el-form>
    <h3 style="text-align: center;">{{title}}</h3>
    <div style="margin-bottom: 10px; overflow: hidden;">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <jq-table :tableData="tableData.list" :sum-tool="true"  :sum-index="tableData.sumIndex">
     <template name="th" v-slot:thGroup>
        <th v-for="(item,index) in tableData.th">{{ item }}</th>
      </template>
      <!-- v-slot:td-${xxx}数据key的名称 -->
<!--     <template name="td" v-slot:td-name="rows">
        <td>自定义姓名{{rows.prop.id}}</td>
      </template> -->
    </jq-table>
<!--
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

  </div>
</template>

<script>
import { listProject , addProject, updateProject, exportProject } from "@/api/payfee/project";
import ysmx from "./json/ysmx.js"
import qfmx from "./json/qfmx.js"
import srtj from "./json/srtj.js"
import srhz from "./json/srhz.js"
export default {
  name: "Project",
  data() {
    return {
      tableData: ysmx,
      // 导出遮罩层
      exportLoading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用类型表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: null,
        projectType: null,
        explains: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectCode: [
          { required: true, message: "代码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    switch (this.$route.params.id){
      case 'qfmx':
        this.tableData = ysmx;
        this.title = '欠费明细表';
        break;
      case 'ysmx':
        this.tableData = srtj;
        this.title = '收款明细表';
        break;
      case 'srtj':
          this.tableData = srtj;
          this.title = '收款汇总表';
          break;
      case 'srhz':
          this.tableData = srtj;
          this.title = '收款汇总表';
          break;
      case 'qfhz':
            this.tableData = ysmx;
            this.title = '欠费汇总表';
            break;

      default:
        break;
    }
    this.getList();
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    },
    /** 查询费用类型列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectCode: null,
        projectType: null,
        explains: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProject(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$confirm('是否确认删除费用类型编号为"' + projectIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProject(projectIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有费用类型数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportProject(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
